package com.prs.crm.report;

import java.io.FileOutputStream;
import java.io.OutputStream;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.log4j.Logger;

/**
 * Excel导出,采用jxl
 * 
 * @author prs.chenzhi
 * 
 */
public class ExcelExport implements ReportExport {

	private static final Logger log = Logger.getLogger(ExcelExport.class);

	

	

	public void export(ReportTable table, String out, ExportConfig config) {
		try {
			OutputStream output = new FileOutputStream(out);
			WritableWorkbook workbook = Workbook.createWorkbook(output);
		    System.out.println("exportTable:"+table.getName());
			WritableSheet sheet = workbook.createSheet(table.getName(), 0);

			renderBody(table, config, sheet);
			workbook.write();
			workbook.close();
			output.close();
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		}
	}

	/**
	 * 销售订单
	 */
	public void exportSaleOrder(ReportTable table, String out,
			ExportConfig config) {
		try {
			OutputStream output = new FileOutputStream(out);
			WritableWorkbook workbook = Workbook.createWorkbook(output);
			WritableSheet sheet = workbook.createSheet(table.getName(), 0);

			renderBodySaleOrder(table, config, sheet);
			workbook.write();
			workbook.close();
			output.close();
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		}
	}

	/**
	 * 销售订单
	 * 
	 * @param table
	 * @param config
	 * @param sheet
	 * @throws Exception
	 */
	private void renderBodySaleOrder(ReportTable table, ExportConfig config,
			WritableSheet sheet) throws Exception {
		/**
		 * render head
		 */
		int i = 0, j = 1;
		for (ReportCell cell : table.getCells()) {
			sheet.setColumnView(i, 13);
			sheet.addCell(new Label(i++, 0, cell.getName(), setStyle()));
		}

		for (Object data : table.getDataSource()) {
			i = 0;
			for (ReportCell cell : table.getCells()) {

				if (cell.getMap() != null) {
					if("salesCodeN".equals(cell.getProperty())){
						sheet.setColumnView(i,26);
					}else{
						sheet.setColumnView(i,13);
					}
					String key = ReportUtils.getValue(data, cell.getProperty(),
							cell.getFormat());
					String value = !key.equals("") ? cell.getMap()
							.get(key.trim()).toString() : cell.getMap().get(
							"null") == null ? "" : cell.getMap().get("null");
					sheet.addCell(new Label(i++, j, value, setStyle()));

				} else if (cell.getSuffix() != ' ') {
					if("salesCodeN".equals(cell.getProperty())){
						sheet.setColumnView(i,26);
					}else{
						sheet.setColumnView(i,13);
					}
					char suffix = cell.getSuffix();
					String value = ReportUtils.getValue(data,
							cell.getProperty(), cell.getFormat());
					if ("productNum".equals(cell.getProperty())) {
						value = value.equals("") ? value : value + suffix;
						jxl.write.Number number = new jxl.write.Number(i++, j,
								Double.parseDouble(value), setStyleMM());
						sheet.addCell(number);

					}

					else {
						if("salesCodeN".equals(cell.getProperty())){
							sheet.setColumnView(i,26);
						}else{
							sheet.setColumnView(i,13);
						}
						value = value.equals("") ? value : value + suffix;
						
						
						sheet.addCell(new Label(i++, j, value, setStyle()));
					}

				} else {
					sheet.setColumnView(i,13);
					sheet.addCell(new Label(i++, j, ReportUtils.getValue(data,
							cell.getProperty(), cell.getFormat()), setStyle()));

				}
			}
			j++;
		}
	}

	/**
	 * 配送单
	 */
	public void exportAlreadyDistribution(ReportTable table, String out,
			ExportConfig config) {
		try {
			OutputStream output = new FileOutputStream(out);
			WritableWorkbook workbook = Workbook.createWorkbook(output);
			WritableSheet sheet = workbook.createSheet(table.getName(), 0);

			renderBodyDistribution(table, config, sheet);
			workbook.write();
			workbook.close();
			output.close();
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		}
	}

	/**
	 * 配送单
	 */

	private void renderBodyDistribution(ReportTable table, ExportConfig config,
			WritableSheet sheet) throws Exception {
		/**
		 * render head
		 */
		int i = 0, j = 1;
		for (ReportCell cell : table.getCells()) {
			// 设置宽度
			sheet.setColumnView(i, 13);
			// 设置高度
			// sheet.setRowView(i, 21);
			sheet.addCell(new Label(i++, 0, cell.getName(), setStyle()));
		}

		for (Object data : table.getDataSource()) {
			i = 0;
			for (ReportCell cell : table.getCells()) {

				if (cell.getMap() != null) {
					String key = ReportUtils.getValue(data, cell.getProperty(),
							cell.getFormat());
					String value = !key.equals("") ? cell.getMap()
							.get(key.trim()).toString() : cell.getMap().get(
							"null") == null ? "" : cell.getMap().get("null");
					sheet.addCell(new Label(i++, j, value, setStyle()));

				} else if (cell.getSuffix() != ' ') {
					
					if("discode".equals(cell.getProperty())){
						sheet.setColumnView(i,23);
					}else{
						sheet.setColumnView(i,20);
					}
					
					char suffix = cell.getSuffix();
					String value = ReportUtils.getValue(data,
							cell.getProperty(), cell.getFormat());

					if ("disTotal".equals(cell.getProperty())) {

						value = value.equals("") ? value : value + suffix;
						jxl.write.Number number = new jxl.write.Number(i++, j,
								Double.parseDouble(value), setStyleM());
						sheet.addCell(number);

					} else if ("proNumber".equals(cell.getProperty())) {

						value = value.equals("") ? value : value + suffix;
						jxl.write.Number number = new jxl.write.Number(i++, j,
								Double.parseDouble(value), setStyleMM());
						sheet.addCell(number);
					} else if ("proPrice".equals(cell.getProperty())) {
						value = value.equals("") ? value : value + suffix;
						jxl.write.Number number = new jxl.write.Number(i++, j,
								Double.parseDouble(value), setStyleMM());
						sheet.addCell(number);
					} else if("discode".equals(cell.getProperty())){
						value = value.equals("") ? value : value + suffix;
						sheet.addCell(new Label(i++, j,value, setStyle()));
					} 
					
					
					else {
						value = value.equals("") ? value : value + suffix;

						sheet.addCell(new Label(i++, j, value, setStyle()));
					}

				} else {
					sheet.addCell(new Label(i++, j, ReportUtils.getValue(data,
							cell.getProperty(), cell.getFormat()), setStyleMM()));

				}
			}
			j++;
		}
	}

	private void renderBody(ReportTable table, ExportConfig config,
			WritableSheet sheet) throws Exception {
		/**
		 * render head
		 */
		int i = 0, j = 1;
		for (ReportCell cell : table.getCells()) {
			// 设置宽度
			sheet.setColumnView(i, 94);
			// 设置高度
			sheet.setColumnView(i, 24);
			sheet.addCell(new Label(i++, 0, cell.getName(), setStyle()));
		}

		for (Object data : table.getDataSource()) {
			i = 0;
			for (ReportCell cell : table.getCells()) {

				if (cell.getMap() != null) {

					String formats="";
					if(cell.getFormat()==null){
						
					}else{
						formats=cell.getFormat();
					}
					String key = ReportUtils.getValue(data, cell.getProperty(),formats);
		
					String keyValue="";
					if(((String)cell.getMap().get(key.trim()))==null){
						
					}else{
						keyValue=((String)cell.getMap().get(key.trim())).toString();
					}
					
					String value = key.equals("") ? cell.getMap().get("null") != null ? (String)cell.getMap().get("null") : "" : keyValue;
					sheet.addCell(new Label(i++, j, value, setStyle()));
				

				} 
				else if (cell.getSuffix() != ' ') {
					char suffix = cell.getSuffix();
					String value = ReportUtils.getValue(data,
							cell.getProperty(), cell.getFormat());
					System.out.println("XX1:"+value);
					if (value.matches("^[\\+\\-]?\\d*?\\.?\\d*?$")) {
						
						value = value.equals("") ? value : value + suffix;
						double keyV=0.0;
						if("".equals(value)){
							
						}else{
							keyV=Double.parseDouble(value);
						}
						jxl.write.Number number = new jxl.write.Number(i++, j,
								keyV, setStyle());
						sheet.addCell(number);
					} else {
						value = value.equals("") ? value : value + suffix;

						sheet.addCell(new Label(i++, j, value, setStyle()));
					}

				} else {
					sheet.addCell(new Label(i++, j, ReportUtils.getValue(data,
							cell.getProperty(), cell.getFormat()), setStyle()));

				}
			}
			j++;
		}
	}

	public WritableCellFormat setStyle() throws Exception {
		WritableFont font1 = new WritableFont(WritableFont.TIMES, 14,
				WritableFont.NO_BOLD);
		WritableCellFormat format1 = new WritableCellFormat(font1);
		format1.setAlignment(Alignment.CENTRE);
		return format1;
	}

	public WritableCellFormat setStyleM() throws Exception {
		WritableFont font1 = new WritableFont(WritableFont.TIMES, 14,
				WritableFont.NO_BOLD);
		WritableCellFormat format1 = new WritableCellFormat(font1);
		format1 = new WritableCellFormat(new NumberFormat("￥0.00"));
		format1.setAlignment(Alignment.CENTRE);
		format1.setFont(font1);
		return format1;
	}

	public WritableCellFormat setStyleMM() throws Exception {
		WritableFont font1 = new WritableFont(WritableFont.TIMES, 14,
				WritableFont.NO_BOLD);
		WritableCellFormat format1 = new WritableCellFormat(font1);
		format1.setAlignment(Alignment.CENTRE);
		return format1;
	}

	

}
